package com.liuyoung.clockview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import java.util.Calendar;

/**
 * Created by Administrator on 2017/2/27.
 */

public class WeatherView  extends View {

    final String BASE_WEATHER_URL = "http://api.map.baidu.com/telematics/v3/weather?ak=HVfYAX5kLGgOdnhov7Zren59z4zcXMmt&mcode=63:FD:8A:D1:DF:52:9E:6A:48:88:7B:1B:FC:41:B8:84:BA:9E:5C:60;com.aplid.happinesslauncher&output=json&location=";


    private Paint mPaint;
    private Paint mTextPaint;

    private Bitmap mWeatherBitmap;

    private int mWeatherWidth;
    private int mWeatherHeight;

    public WeatherView(Context context) {
        super(context);
        initCalendar();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//抗锯齿
    }


    public WeatherView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCalendar();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//抗锯齿
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStrokeWidth(6);
        mTextPaint.setTextSize(50);
        mTextPaint.setColor(Color.WHITE);
    }

    private void initCalendar() {
        mWeatherBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.weather);
        measuereBitmap();
    }

    private void measuereBitmap() {
        mWeatherWidth = mWeatherBitmap.getWidth();
        mWeatherHeight = mWeatherBitmap.getHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mWeatherBitmap, 0, 0, mPaint);
        canvas.save();

        String weatherString="晴 6℃";
        Rect bounds = new Rect();
        mTextPaint.getTextBounds(weatherString,0,weatherString.length(),bounds);
        Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
        int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
        canvas.drawText(weatherString,getMeasuredWidth() / 2 - bounds.width() / 2, baseline, mTextPaint);
        canvas.save();

    }

    private int measureWidth(int mode, int width) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                mWeatherWidth = width;
                mWeatherBitmap   = Bitmap.createScaledBitmap(mWeatherBitmap,mWeatherWidth,mWeatherHeight,true);
                break;
        }
        return mWeatherWidth;
    }

    private int measureHeight(int mode, int height) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                mWeatherHeight = height;
                break;
        }
        return mWeatherHeight;
    }

}
